package mumu.android.lib.localstorage;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class NewsDAO {
	private SQLiteDatabase db;

	static NewsDAO bookDAO;

	public static NewsDAO Instance(Context mContext) {
		if (bookDAO == null) {
			bookDAO = new NewsDAO(mContext);
		}
		return bookDAO;
	}

	private NewsDAO(Context context) {
		DBHelper helper = DBHelper.getHelper(context);
		db = helper.getWritableDatabase();
	}

	static String Table_Name = "news";

	public static final String CREATE_SQL = "create table " + Table_Name + " ( id int   primary key ,  newsid int, newstitle varchar(200), defaultlogo varchar(400) ) ";

	/**
	 * 添加记录
	 * 
	 * @param student
	 */
	public void add(NewsEn en) {
		try {
			ContentValues contentValues = new ContentValues();
			contentValues.put("newsid", en.newsid);
			contentValues.put("newstitle", en.newstitle);
			contentValues.put("defaultlogo", en.defaultlogo);
			db.insert(Table_Name, null, contentValues);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}

	/**
	 * 批量数据添加
	 * 
	 * @param newlist
	 */
	public void add(List<NewsEn> newlist) {
		db.beginTransaction(); // 开始事务
		try {
			for (NewsEn en : newlist) {
				ContentValues contentValues = new ContentValues();
				contentValues.put("newsid", en.newsid);
				contentValues.put("newstitle", en.newstitle);
				contentValues.put("defaultlogo", en.defaultlogo);
				db.insert(Table_Name, null, contentValues);
			}
			db.setTransactionSuccessful(); // 设置事务成功完成
		} finally {
			db.endTransaction(); // 结束事务
		}
	}

	/**
	 * 删除记录
	 * 
	 * @param person
	 */
	public void delete(int newsid) {
		try {
			db.execSQL("delete from  " + Table_Name + "  where newsid=?", new Object[] { newsid });
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}

	/**
	 * 删除记录
	 * 
	 * @param person
	 */
	public void update(String newtitle, int newsid) {
		try {
			ContentValues contentValues = new ContentValues();
			contentValues.put("newstitle", newtitle);
			db.update(Table_Name, contentValues, "newsid=?", new String[] { newsid + "" });
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}

	public List<NewsEn> getList(int newsid) {
		List<NewsEn> list = new ArrayList<NewsEn>();
		Cursor cursor = null;
		try {
			String sql = "select * from " + Table_Name;
			sql += " where newsid= " + newsid;
			cursor = db.rawQuery(sql, new String[] {});
			while (cursor.moveToNext()) {
				NewsEn newsEn = new NewsEn();
				newsEn.newsid = cursor.getInt(1);
				newsEn.newstitle = cursor.getString(2);
				newsEn.defaultlogo = cursor.getString(3);
				list.add(newsEn);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null)
				cursor.close();
		}
		return list;
	}

	/**
	 * 通过content provider 访问数据库
	 * 
	 * @param id
	 */
	public static NewsEn getModelProvider(Context mContext) {
		NewsEn p = null;
		Cursor c = null;
		try {
			c = mContext.getContentResolver().query(NewsTable.CONTENT_URI,
					new String[] { NewsTable.COLUMN_ID, NewsTable.COLUMN_NEWSID, NewsTable.COLUMN_NEWSTITLE, NewsTable.COLUMN_DEFAULTLOGO }, null, null, null);
			if (c != null && c.moveToFirst()) {
				p = new NewsEn();
				p.newsid = c.getInt(c.getColumnIndexOrThrow(NewsTable.COLUMN_NEWSID));
				p.newstitle = c.getString(c.getColumnIndexOrThrow(NewsTable.COLUMN_NEWSTITLE));
				p.defaultlogo = c.getString(c.getColumnIndexOrThrow(NewsTable.COLUMN_DEFAULTLOGO));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (c != null)
				c.close();
		}
		return p;
	}
}
